package com.ruoyi.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateRangeExample {

    public static void main(String[] args) {
        // 获取当前日期
        Date currentDate = new Date();

        // 本周的日期范围
        Date startOfWeek = getStartOfWeek(currentDate);
        Date endOfWeek = getEndOfWeek(currentDate);

        // 本月的日期范围
        Date startOfMonth = getStartOfMonth(currentDate);
        Date endOfMonth = getEndOfMonth(currentDate);

        // 半年的日期范围
        Date startOfHalfYear = getStartOfHalfYear(currentDate);
        Date endOfHalfYear = getEndOfHalfYear(currentDate);

        // 本年的日期范围
        Date startOfYear = getStartOfYear(currentDate);
        Date endOfYear = getEndOfYear(currentDate);

        // 打印结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("本周：" + sdf.format(startOfWeek) + " 到 " + sdf.format(endOfWeek));
        System.out.println("本月：" + sdf.format(startOfMonth) + " 到 " + sdf.format(endOfMonth));
        System.out.println("半年：" + sdf.format(startOfHalfYear) + " 到 " + sdf.format(endOfHalfYear));
        System.out.println("本年：" + sdf.format(startOfYear) + " 到 " + sdf.format(endOfYear));
    }

    public static Date getStartOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DAY_OF_MONTH, 1 - dayOfWeek); // 设置为本周第一天
        setStartTime(calendar);
        return calendar.getTime();
    }

    public static Date getEndOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DAY_OF_MONTH, 7 - dayOfWeek); // 设置为本周最后一天
        setEndTime(calendar);
        return calendar.getTime();
    }

    public static Date getStartOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为本月第一天
        setStartTime(calendar);
        return calendar.getTime();
    }

    public static Date getEndOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为本月最后一天
        setEndTime(calendar);
        return calendar.getTime();
    }

    public static Date getStartOfHalfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        if (month < Calendar.JULY) {
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
        } else {
            calendar.set(Calendar.MONTH, Calendar.JULY);
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为半年第一天
        setStartTime(calendar);
        return calendar.getTime();
    }

    public static Date getEndOfHalfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        if (month < Calendar.JULY) {
            calendar.set(Calendar.MONTH, Calendar.JUNE);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为半年最后一天
        } else {
            calendar.set(Calendar.MONTH, Calendar.DECEMBER);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为半年最后一天
        }
        setEndTime(calendar);
        return calendar.getTime();
    }

    public static Date getStartOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为本年第一天
        setStartTime(calendar);
        return calendar.getTime();
    }

    public static Date getEndOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为本年最后一天
        setEndTime(calendar);
        return calendar.getTime();
    }

    public static void setStartTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
    }

    public static void setEndTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
    }
}
